#include "space.hh"
#include<list>

Vertex::Vertex(float x, float y, float z, unsigned int idx)
   {
      _x = x; _y = y; _z = z; _idx = idx;
   } 

UnitCube::UnitCube(unsigned int baseIdx,
	    unsigned int widthVertNums,
	    unsigned int heightVertNums)
    {
       _vertIdx[0] = baseIdx;
       _vertIdx[1] = _vertIdx[0] + 1;
       _vertIdx[2] = _vertIdx[0] + widthVertNums;
       _vertIdx[3] = _vertIdx[2] + 1;
  
       _vertIdx[4] = baseIdx + widthVertNums * heightVertNums;
       _vertIdx[5] = _vertIdx[4] + 1;
       _vertIdx[6] = _vertIdx[4] + widthVertNums;
       _vertIdx[7] = _vertIdx[6] + 1;
    }


bool NewSpace( float unitSize, int xCubeNums, int yCubeNums, int zCubeNums,
	       std::vector<Vertex>& vertVector,
	       std::vector<UnitCube>& cubeVector)
{
   bool success = true;
   unsigned int curVertIdx = 0;
   int xVertNums = xCubeNums+1;
   int yVertNums = yCubeNums+1;
   int zVertNums = zCubeNums+1;

   
   for(int y=0; y<yVertNums; y++)
   {
      for(int z=0; z<zVertNums; z++)
      {
	 for(int x=0; x<xVertNums; x++)
	 {
	    Vertex v(x*unitSize, y*unitSize, z*unitSize, curVertIdx);
	    vertVector.push_back(v);

	    if(x!=xVertNums-1 &&
	       y!=yVertNums-1 &&
	       z!=zVertNums-1)
	    {
	       UnitCube uc(curVertIdx, xVertNums, zVertNums);
	       cubeVector.push_back(uc);
	    }
	    curVertIdx++;
	 }
      }
   }
   //reset
   curVertIdx = 0;

   // unsigned int unitBaseIdx = 0;
   // for(int y=0; y<yCubeNums; y++)
   // {
   //    unitBaseIdx += zVertNums * y;
   //    for(int z=0; z<zCubeNums; z++)
   //    {
   // 	 unitBaseIdx += xVertNums*z;
   // 	 for(int x=0; x<xCubeNums; x++)
   // 	 {
   // 	    unitBaseIdx += x;

   // 	    UnitCube uc(unitBaseIdx, xVertNums, zVertNums);
   // 	 }
   //    }
   // }

   return success;
}


void AttachCubeToDrawList(const UnitCube& cube, std::list<GLuint>& list)
{
   const unsigned int* p = cube._vertIdx;
   GLuint drawArray[24] = {
      p[0], p[4], p[6], p[2],  //left-side
      p[0], p[1], p[5], p[4],  //front-side
      p[1], p[3], p[7], p[5],  //right-side
      p[3], p[2], p[6], p[7],  //back-side
      p[4], p[5], p[7], p[6],  //top-side
      p[2], p[3], p[1], p[0],  //bottom-side
   };

   for(int i=0; i<24; i++)
   {
      list.push_back(drawArray[i]);
   }
}
